<?php
/**
* @version		$Id: turist_infos.php 3 2011-10-28 15:49:24Z netix.mail@gmail.com $
* @package		ALM Hotel Menager	
* @subpackage	com_almhm
* @author		ALM Studio
* @copyright	Copyright 2011 ALM Studio. All rights reserved
* @licence		Commercial
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHmModelTurist_infos extends JModelList
{
	
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'TinfoId', 'a.TinfoId',
				'TinfoMjesto', 'a.TinfoMjesto',
				'TinfoNaziv', 'a.TinfoNaziv',
				'TinfoSlika', 'a.TinfoSlika',
				'TinfoOpis', 'a.TinfoOpis',
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}
		
		$mjestoId = $app->getUserStateFromRequest($this->context.'.filter.mjesto_id', 'filter_mjesto_id');
		$this->setState('filter.mjesto_id', $mjestoId);

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
	
		
		parent::populateState('a.TinfoId', 'desc');		
				
	}
	
	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);

		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almhm_turist_info AS a');
		
		$mjestoId = $this->getState('filter.mjesto_id');
		if (is_numeric($mjestoId)) {
			$query->where('a.TinfoMjesto = '.$mjestoId);
		}
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'TinfoNaziv:') === 0) {
				$query->where('a.TinfoNaziv = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.TinfoNaziv LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.TinfoId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
	
	public function getMjesto() {
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('m.MjestoId AS value, m.MjestoNaziv AS text');
		$query->from('#__almhm_mjesta AS m');
		$query->join('INNER', '#__almhm_turist_info AS t ON t.TinfoMjesto = m.MjestoId');
		$query->group('m.MjestoId');
		$query->order('m.MjestoNaziv');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}
}


